$(function () {
    var isEdit = getParmas(location.href).isEdit;
    if (isEdit == 1) {
        if (localStorage.getItem('editAddress')) {
            var address = JSON.parse(localStorage.getItem('editAddress'))
            var id = address.id;
            $('#form').html(template('editTemp', address))
        }
    } else {
        $('#form').html(template('editTemp', {}))
    }
    //创建选择器-3级联动
    var picker = new mui.PopPicker({ layer: 3 });
    //加载数据，数据是数组--数据在city文件中
    picker.setData(cityData);
    $('#selectCity').on('tap', function () {
        //显示
        picker.show(function (ele) {
            $('#selectCity').val(ele[0].text + ele[1].text + ele[2].text)
        })
    })
    //添加收货地址
    $('#address').on('tap', function () {
        var recipients = $('[name=username]').val().trim();
        var postcode = $('[name=postCode]').val().trim();
        var address = $('[name=city]').val().trim();
        var addressDetail = $('[name=detail]').val().trim();
        if (!recipients) {
            mui.toast('请输入收货姓名')
            return
        }
        if (!postcode) {
            mui.toast('请输入邮政编码')
            return
        }
        if (!address) {
            mui.toast('请选择地址')
            return
        }
        if (!addressDetail) {
            mui.toast('请输入详细地址')
            return
        }
        var data = {
            recipients,
            postcode,
            address,
            addressDetail
        };
        if (isEdit == 1) {
            // 编辑操作
            var url = "/address/updateAddress";
            data.id = id;
        } else {
            // 添加操作
            var url = "/address/addAddress";
        }
        $.ajax({
            type: "post",
            url,
            data,
            success: function (res) {
                console.log(res);
                if (res.success) {
                    location.href = 'address.html'
                }
            }
        });



    })

})